#include "udp_client_plus.h"

namespace evnetplus {

UDPClientPlus::UDPClientPlus(EventLoop* loop,
                             const std::string& remote_addr,
                             const std::string& name)
    : _loop(loop)
    , _client(new UDPClient(loop, remote_addr, name))
    , _ticker_helper(TimeTickerHelper::Ptr(new TimeTickerHelper())) {
    
    _client->SetMessageCallback(std::bind(&UDPClientPlus::onReceive, this, std::placeholders::_1));

    _ticker_helper->timer = InvokeTimer::Create(_loop, Duration(1.0F), [this]{
        onTimerHandle();
    }, true);
}

UDPClientPlus::~UDPClientPlus() {}

void UDPClientPlus::create() {
    _client->Create();
    _ticker_helper->timer->Start();
}

void UDPClientPlus::destroy() {
    _client->Destroy();
    _ticker_helper->timer->Cancel();
}

void UDPClientPlus::sendMessage(const Slice& message) {
    _client->SendMsg(message);
}

}
